package com.can.Controller;

import com.can.Service.GoodBadService;
import com.can.Service.PostService;
import com.can.Service.ReplyService;
import com.can.domain.Dto.CreatePostDto;
import com.can.domain.Dto.CreateReplyDto;
import com.can.domain.vo.postcommonvo;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/post")
public class postcontroller {
    @Autowired
    PostService postservice;
    @Autowired
    ReplyService replyservice;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    GoodBadService goodBadService;

    //    获取帖子列表
    @RequestMapping("/getPostsBySectionId/{id}")
    @ResponseBody
    public postcommonvo getPostsBySectionId(@PathVariable (value = "id")int id) {
        postcommonvo res=postservice.getPostsBySectionId(id);
        System.out.println(res);
        return res;
    }

    //    获取回复列表
    @RequestMapping("/getRepliesByPostId/{postid}")
    @ResponseBody
    public postcommonvo getRepliesByPostId(@PathVariable (value = "postid")int postid) {
        postcommonvo res=replyservice. getRepliesByPostId(postid);
//        System.out.println(res);
        return res;
    }

    //    查一楼
    @RequestMapping("/getPostByPostId/{postid}")
    @ResponseBody
    public postcommonvo getPostByPostId(@PathVariable (value = "postid")int postid) {
        postcommonvo res=postservice. getPostByPostId(postid);
//        System.out.println(res);
        return res;
    }


    @PostMapping("/deletepost/{id}")
    @ResponseBody
//    删除帖子
    public postcommonvo deletePost(HttpServletRequest request, @PathVariable(value = "id") int postid) {
        System.out.println("权限是");
        String power = request.getHeader("X-Power") ;
        System.out.println(power);
        if (power.equals("0")){
            return new postcommonvo<>(403,"权限错误",null);
        }else {
            return postservice.deletePosById(postid);
        }

    }
//

    //    发布帖子
    @PostMapping("/createPost")
    @ResponseBody
    public postcommonvo createPost(@RequestBody CreatePostDto releasedto, HttpServletRequest request) throws Exception {
        // 1.获取请求头中的用户信息
        String userId = request.getHeader("X-UserId") ;
        String power = request.getHeader("X-Power") ;
        System.out.print(userId);
        System.out.print(power);
        System.out.println(releasedto.toString());
        return postservice.createPost(Integer.parseInt(userId),releasedto);

    }

    //    发布一级回复
    @PostMapping("/createReply")
    @ResponseBody
    public postcommonvo createReply(@RequestBody CreateReplyDto createreplydto, HttpServletRequest request) throws Exception {
        // 1.获取请求头中的用户信息
        System.out.println("进入控制器");
        String userId = request.getHeader("X-UserId") ;
        String username = request.getHeader("X-Username") ;
//        加入token的信息到表单类
        createreplydto.setUsername(username);
        createreplydto.setUserId(Integer.parseInt(userId));
        return replyservice.CreateReply(createreplydto);

    }

    @GetMapping("/getAllGoodBad")   //从redis取出所有点赞踩数据给前端初始化
    @ResponseBody
    public postcommonvo getAllGoodBad() {

        return goodBadService.getAllGoodBad();
    }

    @PostMapping("/goodById/{id}")   //点赞
    @ResponseBody
    public postcommonvo goodById(@PathVariable(value = "id") int id) {
        return goodBadService.goodById(id);
    }

    @PostMapping("/badById/{id}")   //踩
    @ResponseBody
    public postcommonvo badById(@PathVariable(value = "id") int id) {
        return goodBadService.badById(id);
    }
//    @PostMapping("/{postId}/like")
//    public postcommonvo likePost(@PathVariable("postId") Long postId) {
//        String goodKey = "post:" + postId + ":good";
//        String badKey = "post:" + postId + ":bad";
//
//        // 1. 如果 Redis 没有数据，从数据库查询并初始化
//        if (!redisTemplate.hasKey(goodKey) || !redisTemplate.hasKey(badKey)) {
//            initFromDatabase(postId);
//
//        // 2. 增加点赞数
//        redisTemplate.opsForValue().increment(goodKey);
//
//        // 3. 获取当前点赞和踩数
//        int good = Integer.parseInt(redisTemplate.opsForValue().get(goodKey));
//        int bad = Integer.parseInt(redisTemplate.opsForValue().get(badKey));
//
//        // 4. 可选：实时同步到数据库（如果对一致性要求高）
//        syncToDatabase(postId, good, bad);
//
//        // 5. 返回结果
//        Map<String, Integer> response = new HashMap<>();
//        response.put("good", good);
//        response.put("bad", bad);
//
//        return ResponseEntity.ok(response);
//    }


}
